const express = require("express");
const mysql = require("mysql");
const cors = require("cors");
const session = require("express-session");

var pool = mysql.createPool({
	host: "127.0.0.1",
	user: "root",
	password: "",
	database: "tedu_MicroBlog",
	pore: "3306",
	connectionLimit: 20
});
var server = express();
server.use(cors({
	origin: ["http://127.0.0.1:8080", "http://localhost:8080", "http://127.0.0.1:80", "http://localhost:80"],
	credentials: true
}));
server.use(session({
	secret: "128位安全字符串",
	resave: true,
	saveUninitialized: true
}));
server.use(express.static("public"));
server.listen(3000);
//登录
server.get("/login", (req, res) => {
	var obj = req.query;
	var sql = "select uid from wcd_user where uname = ? and upwd= ?";
	pool.query(sql, [obj.uname, obj.upwd], (err, result) => {
		if (err) throw err;
		if (result.length == 0) {
			res.send({ code: -1, msg: "用户名或密码错误" });
		} else {
			// 4.1登录成功后会将登录凭证id保存session对象
			// 4.2获取当前登录用户的id
			var uid = result[0].uid;
			req.session.uid = uid;
			console.log(req.session.uid)
			// 4.3将用户id保存session对象
			res.send({ code: 1, msg: "登录成功" });
		}
	})
})
//重置密码

// 注册

